<template>
  <div class="main">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit"
    >
      <!-- <a-tabs
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick"
      > -->
        <!-- <a-tab-pane key="tab1" tab="账号密码登录"> -->
          <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="请输入帐户名"
              v-decorator="[
                'username',
                {rules: [{ required: true, message: '请输入帐户名' }], validateTrigger: 'change'}
              ]"
            >
              <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input>
          </a-form-item>
          <a-form-item>
            <a-input-password
              size="large"
              type="password"
              autocomplete="false"
              placeholder="请输入密码"
              v-decorator="[
                'password',
                {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input-password>
          </a-form-item>
          <a-form-item>
            <a-input-group compact>
              <a-input
                style="width:68%;margin-right: 2%;"
                size="large"
                type="text"
                placeholder="请输入图形验证码"
                v-decorator="[
                  'captcha',
                  {rules: [{ required: true, message: '请输入图形验证码' }], validateTrigger: 'change'}
                ]"
              >
                <a-icon slot="prefix" type="schedule" :style="{ color: 'rgba(0,0,0,.25)' }"/>
              </a-input>
              <div style="width:30%; cursor: pointer; border: 1px solid #999;">
                <img @click="getCode" :src="codeImg" style="width: 98%;height: 40px;" alt="">
              </div>
            </a-input-group>
          </a-form-item>
        <!-- </a-tab-pane>
        <a-tab-pane key="tab2" tab="手机号登录">
          <a-form-item>
            <a-input size="large" type="text" placeholder="手机号" v-decorator="['mobile', {rules: [{ required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号' }], validateTrigger: 'change'}]">
              <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input>
          </a-form-item>

          <a-row :gutter="16">
            <a-col class="gutter-row" :span="16">
              <a-form-item>
                <a-input size="large" type="text" placeholder="验证码" v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}]">
                  <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }"/>
                </a-input>
              </a-form-item>
            </a-col>
            <a-col class="gutter-row" :span="8">
              <a-button
                class="getCaptcha"
                tabindex="-1"
                :disabled="state.smsSendBtn"
                @click.stop.prevent="getCaptcha"
                v-text="!state.smsSendBtn && '获取验证码' || (state.time+' s')"
              ></a-button>
            </a-col>
          </a-row>
        </a-tab-pane>
      </a-tabs> -->
      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >确定</a-button>
      </a-form-item>
      <div class="register">
        <router-link :to="{ name: 'Register' }">工厂管理员注册</router-link>
        <router-link
          :to="{ name: 'upForgetPassword'}"
        >忘记密码?</router-link>
      </div>
    </a-form>
  </div>
</template>
<script>
import { userLogin, userDetail, getCodeImg } from '@/api/passport';
import { showErrors, showError } from '@/utils/showMsg';
import { mapActions } from 'vuex';

export default {
  data() {
    return {
      loginBtn: false,
      customActiveKey: 'tab1',
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false,
      },
      codeImg: '',
      codeImgkey: '',
    };
  },
  mounted() {
    this.getCode()
  },
  methods: {
    getCode(){
      getCodeImg().then(res=>{
        console.log(res);
        if (res.data.errorCode === 0) {
          this.codeImg = res.data.result.img
          this.codeImgkey = res.data.result.key
        }
      })
    },
    handleTabClick (key) {
      this.customActiveKey = key
      // this.form.resetFields()
    },
    ...mapActions({
      storeLogin: 'user/Login',
      userLogin: 'user/UserLogin',
    }),
    handleSubmit(e) {
      const that = this
      e.preventDefault();
      const { form: { validateFields }, state,} = this;
      // console.log(state);
      state.loginBtn = true;

      const validateFieldsKey = ['username', 'password', 'captcha'];

      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        console.log(values);
        if (!err) {
          const loginParams = { 
            username: values.username, 
            password: values.password ,
            captcha: values.captcha,
            key: this.codeImgkey 
          };
          userLogin(loginParams).then((res) => {
            // console.log(res);
            if (res.data.errorCode === 0) {
              that.storeLogin({ accessToken: res.data.result.access_token });

              userDetail().then((r) => {
                // console.log(r);
                that.userLogin({data:r.data.result});
                that.$router.push({
                  path: '/dashboard',
                })
              })
            } else if (res.data.errorCode === 414) {
              showError(res.data.message);
              this.getCode()
              this.form.setFieldsValue({
                captcha: '',
              });
            } else {
              showError(res.data.message);
            }
          });
          state.loginBtn = false;
        } else {
          setTimeout(() => {
            state.loginBtn = false;
          }, 600);
        }
      });
    },
  },
};
</script>
<style lang="stylus" scoped>
  .user-layout-login
    label
      font-size: 14px;

    .getCaptcha
      display: block;
      width: 100%;
      height: 40px;

    .forge-password
      font-size: 14px;

    button.login-button
      padding: 0 15px;
      font-size: 16px;
      height: 40px;
      width: 100%;

    .user-login-other
      text-align: left;
      margin-top: 24px;
      line-height: 22px;

      .item-icon
        font-size: 24px;
        color: rgba(0, 0, 0, 0.2);
        margin-left: 16px;
        vertical-align: middle;
        cursor: pointer;
        transition: color 0.3s;

        &:hover
          color: #1890ff;

  .main
    .register
      display: flex
      justify-content: space-between
      a
        color: #1890ff;
      a:hover 
        color: #2d8cf0;
        text-decoration: underline;
</style>
